#region Imported Namespaces


using System;
using System.Diagnostics;
using System.Globalization;
using System.Windows.Data;

using WtfManager.Framework;


#endregion


namespace WtfManager.HierarchicalData
{
  ///<summary>
  /// Converts Data hierarchy <see cref="TreeItem">tree items</see> to a path string and back.
  ///</summary>
  [ValueConversion(typeof(TreeItem), typeof(String))]
  public class TreeItemToPathConverter: Service, IValueConverter
  {
    #region Fields


    private static TreeItemToPathConverter _instance;
    private DataHierarchyService _dataHierarchyService;


    #endregion


    #region Constructors/Finalizers


    ///<summary>
    /// Default TreeItemToPathConverter constructor.
    ///</summary>
    internal TreeItemToPathConverter()
    {
    }


    #endregion


    #region Properties/Indexers


    /// <summary>
    /// Gets a description of a service.
    /// </summary>
    public override string Description
    {
      get
      {
        return "Converts Tree Items to path string and back.";
      }
    }


    ///<summary>
    /// Gets an instance of <see cref="TreeItemToPathConverter"/>
    ///</summary>
    public static TreeItemToPathConverter Instance
    {
      get
      {
        return _instance;
      }
    }


    /// <summary>
    /// Gets a name of a service.
    /// </summary>
    public override string Name
    {
      get
      {
        return "Tree Item to Path Converter";
      }
    }


    #endregion


    #region Methods


    protected override void OnStateChnaged()
    {
      if (IsInitialized)
      {
        _instance = this;
        _dataHierarchyService = (DataHierarchyService)WtfFramework.Instance.Services[typeof(DataHierarchyService)];
      }
      else
      {
        _instance = null;
        _dataHierarchyService = null;
      }
    }


    protected override FrameworkComponentState OnLoad()
    {
      AddDependency(typeof(DataHierarchyService));

      return FrameworkComponentState.Loaded;
    }


    protected override void OnUnload()
    {
      _dataHierarchyService = null;
    }


    protected override FrameworkComponentState OnInitialize()
    {
      return FrameworkComponentState.Initialized;
    }


    ///<summary>
    /// Converts a value. 
    ///</summary>
    ///<returns>A converted value. If the method returns null, the valid null value is used.</returns>
    ///<param name="value">The value produced by the binding source.</param>
    ///<param name="targetType">The type of the binding target property.</param>
    ///<param name="parameter">The converter parameter to use.</param>
    ///<param name="culture">The culture to use in the converter.</param>
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
      Debug.Assert(IsInitialized, "TreeItemToPathConverter is not yet initialized.");

      var item = value as TreeItem;

      return item != null ? item.Path : null;
    }


    ///<summary>
    ///Converts a value. 
    ///</summary>
    ///<returns>A converted value. If the method returns null, the valid null value is used.</returns>
    ///<param name="value">The value that is produced by the binding target.<param>
    ///<param name="targetType">The type to convert to.</param>
    ///<param name="parameter">The converter parameter to use.</param>
    ///<param name="culture">The culture to use in the converter.</param>
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
      Debug.Assert(IsInitialized, "TreeItemToPathConverter is not yet initialized.");

      var path = value as string;

      if (string.IsNullOrEmpty(path))
      {
        return null;
      }

      TreeItem item = _dataHierarchyService.Hierarchy.GetItem(path);

      return item;
    }


    #endregion
  }
}